package com.lb.shopguide.ui.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lb.baselib.base.DialogBase;
import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.baselib.http.SimpleObserverOtherListener;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.recommend.RecBannerBean;
import com.lb.shopguide.entity.trace.TraceBean;
import com.lb.shopguide.http.method.ApiMethodGuide;
import com.lb.shopguide.trace.TraceManager;
import com.lb.shopguide.ui.activity.GuideMainActivity;
import com.lb.shopguide.util.json.JsonUtil;
import com.lb.shopguide.util.lb.BizUtil;
import com.lb.shopguide.util.pic.ImageLoaderUtils;
import com.trello.rxlifecycle2.android.FragmentEvent;

/**
 * Create by kangzhao on 2018/11/26
 * share 2 wechat circle
 */
public class DialogShare2Circle extends DialogBase implements View.OnClickListener {
    private ImageView ivBanner;
    private TextView tvCancel;
    private LinearLayout layoutShareCircle;

    private int bannerType;
    private RecBannerBean bean;
    private String productCode;

    public static DialogShare2Circle getDialogShare2Circle(int bannerType, String productCode) {
        DialogShare2Circle dialogShare2Circle = new DialogShare2Circle();
        Bundle bundle = new Bundle();
        bundle.putInt(AppConstant.BANNER_TYPE, bannerType);
        bundle.putString(AppConstant.PRODUCT_CODE, productCode);
        dialogShare2Circle.setArguments(bundle);
        return dialogShare2Circle;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mFragmentView = inflater.inflate(R.layout.dialog_share_to_circle, container, false);
        initData();
        initView(mFragmentView);

        setDialogPosition();
        return mFragmentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, R.style.dialog_bottom_rec);
    }

    private void setDialogPosition() {
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.BOTTOM | Gravity.FILL_HORIZONTAL);
    }


    private void initData() {
        Bundle bundle = getArguments();
        if (bundle.containsKey(AppConstant.BANNER_TYPE)) {
            bannerType = bundle.getInt(AppConstant.BANNER_TYPE);
        }
        if (bundle.containsKey(AppConstant.PRODUCT_CODE)) {
            productCode = bundle.getString(AppConstant.PRODUCT_CODE);
        }
        getAdBanner();
    }

    private void initView(View view) {
        ivBanner = view.findViewById(R.id.iv_banner);
        int width = ScreenUtils.getScreenWidth() - SizeUtils.dp2px(36);
        float aspect = 0.2353f;
        int height = (int) (width * aspect);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ivBanner.getLayoutParams();
        params.width = width;
        params.height = height;
        ivBanner.setLayoutParams(params);
        tvCancel = view.findViewById(R.id.tv_cancel);
        layoutShareCircle = view.findViewById(R.id.layout_share_circle);

        tvCancel.setOnClickListener(this);
        layoutShareCircle.setOnClickListener(this);
        ivBanner.setOnClickListener(this);
        trackShare();
    }

    private void trackShare() {
        TraceBean bean = new TraceBean();
        bean.setXdCode("XD0007");
        bean.setYdCode("YD0002");
        bean.setProductType(""+3);
        bean.setProductCode(productCode);
        TraceManager.getTraceManager(getContext()).trackShareGoods(bean);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                DialogShare2Circle.this.dismissAllowingStateLoss();
                break;
            case R.id.iv_banner:
                if (mOnDialogClickListener != null) {
                    bean.setJumpUrl(BizUtil.fixActivityUrl(bean.getJumpUrl(), "XD0015"));
                    mOnDialogClickListener.onBannerClick(bean);
                }
                break;
            case R.id.layout_share_circle:
                if (mOnDialogClickListener != null) {
                    DialogShare2Circle.this.dismissAllowingStateLoss();
                    mOnDialogClickListener.onShareCircle();
                }
                break;
        }
    }

    private void bindBanner(RecBannerBean bean) {
        ImageLoaderUtils.display(getContext(), ivBanner, bean.getBannerPicUrl());
    }


    private void getAdBanner() {
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == ResponseCode.RESPONSE_SUCCESS) {
                    bean = (RecBannerBean) JsonUtil.getObject(response.getReturnContent(), RecBannerBean.class);
                    bindBanner(bean);
                }
            }
        };
        SimpleObserverOtherListener errorListener = new SimpleObserverOtherListener() {

            @Override
            public void onSessionOutOfDate() {
                ((GuideMainActivity) getActivity()).onSessionOutOfDate();
            }

            @Override
            public void onError(String error) {
                ToastUtils.showShort(error);
            }
        };
        ApiMethodGuide.getAdBanner(bannerType, new SimpleLbObserver(listener, errorListener), this, FragmentEvent.DESTROY_VIEW);
    }


    public interface OnDialogClickListener {
        void onBannerClick(RecBannerBean bean);

        void onShareCircle();
    }

    private OnDialogClickListener mOnDialogClickListener;

    public void setOnDialogClickListener(OnDialogClickListener listener) {
        mOnDialogClickListener = listener;
    }
}
